﻿using DigDes.DSchool.SUPS.DataAccess.Interface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DigDes.DSchool.SUPS.DataAccess.Database
{
    public class SelectorDislocationDAC
    {
        private static readonly string ConnectionStringKey = "SUPSDB";

        public static IEnumerable<DislocationToDisplayDTO> SelectDislocation()
        {
            var connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringKey].ConnectionString;

            using (var sqlConn = new SqlConnection(connectionString))
            {

                var sqlCmd = new SqlCommand("GetDislocation", sqlConn);

                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlConn.Open();
                SqlDataReader reader = sqlCmd.ExecuteReader();
                while (reader.Read())
                {
                    yield return GetDislocationFromReader(reader);
                }


            }

        }

        private static DislocationToDisplayDTO GetDislocationFromReader(SqlDataReader reader)
        {
            DislocationToDisplayDTO row = new DislocationToDisplayDTO();
            if (!reader.IsDBNull(0))
            {
                row.CarNumber = reader.GetInt32(0);
            }
            if (!reader.IsDBNull(1))
            {
                row.Weight = reader.GetDouble(1);
            }
            if (!reader.IsDBNull(2))
            {
                row.ArrivalStationName = reader.GetString(2);
            }
            if (!reader.IsDBNull(3))
            {
                row.CargoName = reader.GetString(3);
            }
            if (!reader.IsDBNull(4))
            {
                row.ConsigneeCode = reader.GetString(4);
            }
            if (!reader.IsDBNull(5))
            {
                row.CarParkType = reader.GetString(5);
            }

            if (!reader.IsDBNull(6))
            {
                row.DispatchStationName = reader.GetString(6);
            }
            if (!reader.IsDBNull(7))
            {
                row.OperationName = reader.GetString(7);
            }

            if (!reader.IsDBNull(8))
            {
                row.OperationDateTime = reader.GetDateTime(8);
            }

            if (!reader.IsDBNull(9))
            {
                row.OperationStationName = reader.GetString(9);
            }

            if (!reader.IsDBNull(10))
            {
                row.RoadOfDeliveryName = reader.GetString(10);
            }

            if (!reader.IsDBNull(11))
            {

                row.RoadOfReceptionName = reader.GetString(11);
            }

            if (!reader.IsDBNull(12))
            {
                row.TrainIndex = reader.GetString(12);
            }

            if (!reader.IsDBNull(13))
            {
                row.TrainNumber = reader.GetString(13);
            }
            return row;

        }
    }
}
